package dhz.storage.controllers;

import dhz.storage.dao.ShareLinkDao;
import dhz.storage.dao.ShareLinkDaoImpl;
import dhz.storage.models.ShareLink;
import dhz.storage.util.Response;
import io.swagger.annotations.*;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.List;

@Api(tags = "分享文件方面的接口")
@RestController
@RequestMapping("/share")
public class ShareControllers {
    @Resource
    ShareLinkDao link_service;

    static final Logger logger = LoggerFactory.getLogger(ShareControllers.class);

    @ApiOperation(value = "生成分享链接接口", notes = "生成分享链接并返回分享id，通过这个东西向后端下载文件，默认七天有效期")
    @ApiResponses({
            @ApiResponse(code = 1001, message = "生成成功"),
            @ApiResponse(code = 500, message = "路径无效")
    })
    @PostMapping("/generate")
    public Response generateShareLink(@RequestParam("path") @ApiParam(value = "被分享的文件的路径") String path){
        String identification = SecurityUtils.getSubject().getPrincipal().toString();
        String file_dir = "/mnt/students/"+identification+path;
        File f = new File(file_dir);
        Response res = new Response(null);
        res.setCode(1001);
        res.setMessage("连接创建成功");
        if(!f.exists()){
            res.setCode(500);
            res.setMessage("路径无效");
            logger.warn(identification+" failed to create share link on "+f.getAbsolutePath()+" which is non-exists");
            return res;
        }
        ShareLink link = new ShareLink(identification, path);
        long id = link_service.save(link);
        res.setData(id);
        logger.info(identification+" created a share link on "+f.getAbsolutePath()+" whose id is "+link.getId());
        return res;
    }

    @ApiOperation(value = "获取分享链接数量", notes = "获取当前用户有多少分享链接，用于计算有多少页")
    @PostMapping("/count")
    public Response countLinks(){
        String identification = SecurityUtils.getSubject().getPrincipal().toString();
        long count = link_service.countLinks(identification);
        Response res = new Response(count);
        res.setCode(1001);
        res.setMessage("计算成功");
        logger.info(identification+" successfully counted his/her number of links which is "+count);
        return res;
    }

    @ApiOperation(value = "列出当前用户的全部分享链接", notes = "展现全部分享链接，按照开始分享的日期排序，需要给出页数，默认每页十条记录")
    @PostMapping("/ls")
    public Response listSharing(@RequestParam("page") @ApiParam("表示查询第几页") int page){
        String identification = SecurityUtils.getSubject().getPrincipal().toString();
        List<ShareLink> links = link_service.findShareLinks(identification, page);
        Response res = new Response(links);
        res.setCode(1001);
        res.setMessage("查询成功");
        return res;
    }

    @ApiOperation(value = "删除这个链接", notes = "删除这个分享链接，意味着提前结束分享")
    @ApiResponses({
            @ApiResponse(code = 1001, message = "删除成功"),
            @ApiResponse(code = 1002, message = "删除失败"),
            @ApiResponse(code = 403, message="用户验证失败")
    })
    @PostMapping("/rm")
    public Response removeLink(@RequestParam("id") @ApiParam("被删除的连接的id") long id){
        String identification = SecurityUtils.getSubject().getPrincipal().toString();
        Response res = new Response(null);
        if(identification == null){
            res.setCode(403);
            res.setMessage("用户验证失败");
            logger.warn("someone tries to delete file illegally");
            return res;
        }
        boolean deleted = link_service.remove(id);
        res.setCode(deleted?1001:1002);
        res.setMessage(deleted?"删除成功":"没有这个链接");

        return res;
    }
}
